﻿//======================================================================
//
//        Copyright (C) 2015   
//        All rights reserved
//
//        filename : J_ReturnMonthInv
//        description : 技术部退卡盘点统计月报表
//        created by XXX at  20160505
//
//======================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CardLogistics.DAL;
using System.Text;
using CardLogistics.Web.Components;

namespace CardLogistics.Web.MainPage.Report.DepReport
{
    public partial class J_ReturnMonthInv : BasePage
    {
        private static readonly DepReportData DepDAL = new DepReportData();

        #region 查询
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Search_Click(object sender, EventArgs e)
        {
            if (txt_Data.Text.Trim().Length > 0)
            {
                string startV = txt_Data.Text + "-01";//统计月份的第一天
                string endV = DateTime.Parse(startV).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");//统计月份的最后一天
                BindData(startV, endV);
            }
            else
                ShowMessageBox("请选择统计月份！");
                txt_Data.Focus();
        }
        #endregion

        #region 数据绑定
        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="startData"></param>
        /// <param name="endData"></param>
        private void BindData(string startData, string endData)
        {
            DataTable dt = DepDAL.GetJReturnMonthSta(startData, endData);

            StringBuilder sbStr = new StringBuilder();
            sbStr.Append("<div style='text-align: center; font-size: 25px; font-weight: bold'>西安城市一卡通有限责任公司卡片统计月报表</div>");
            sbStr.Append("<table  style='border:0;width:100%;'><tr><td colspan='5' style='border:0;text-align:left;'>填报单位:</td><td  colspan='" + (dt.Rows.Count * 3 - 4) + "' style='border:0;text-align:right;'>制表日期:" + DateTime.Now.ToString("yyyy年MM月") + "</td></tr></table>");
            sbStr.Append("<table  border='1' style='font-size:16px;white-space: nowrap; '>");
            StringBuilder sbThCard = new StringBuilder("<tr><td rowspan='2'colspan='2'></td>");//卡类型名称
            StringBuilder sbThNPT = new StringBuilder("<tr>");//数量、单据、总计
            StringBuilder sbThSQ_TH = new StringBuilder("<tr><td rowspan='3' style='width:90px;'>上期结存</td><td style='width:80px;'>退换问题卡</td>");//上期结存-退换问题卡
            StringBuilder sbThSQ_LY = new StringBuilder("<tr><td>二次利用卡</td>");//上期结存-二次利用卡
            StringBuilder sbThSQ_ZKHK = new StringBuilder("<tr><td>二次制卡坏卡</td>");//上期结存-二次制卡坏卡

            StringBuilder sbThBQ_RKTH = new StringBuilder("<td colspan='2'>本期入库退换问题卡</td>");//本期入库退换问题卡
            StringBuilder sbThBQ_FCHK = new StringBuilder("<td colspan='2'>本期返回厂家坏卡</td>");//本期返回厂家坏卡
            StringBuilder sbTHBQ_HS = new StringBuilder("<td colspan='2'>本期二次利用回收</td>");//本期二次利用回收
            StringBuilder sbTHBQ_CK = new StringBuilder("<td colspan='2'>本期二次利用出库</td>");//半期二次利用出库
            StringBuilder sbTHBQ_ZKHK = new StringBuilder("<td colspan='2'>本期二次利用制卡坏卡</td>");//本期二次利用制卡坏卡
            StringBuilder sbTHBQ_XHRK = new StringBuilder("<td colspan='2'>本期销毁入库</td>");//本期本期销毁入库

            StringBuilder sbThKC_TH = new StringBuilder("<tr><td rowspan='3'>库存</td><td>退换问题卡</td>");//库存-退换问题卡
            StringBuilder sbThKC_LY = new StringBuilder("<tr><td>二次利用卡</td>");//库存-退换问题卡
            StringBuilder sbThKC_ZKHK = new StringBuilder("<tr><td>二次制卡坏卡</td>");//库存-退换问题卡


            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sbThCard.Append("<td colspan='3'>" + dt.Rows[i]["cardtypename"].ToString() + "</td>");
                sbThNPT.Append("<td style='width:90px;'>数 量(张)</td><td style='width:90px;'>单 价(元)</td><td style='width:90px;'>金 额(元)</td>");
                sbThSQ_TH.Append("<td>" + dt.Rows[i]["上期退换问题卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["上期退换问题卡金额"].ToString() + "</td>");
                sbThSQ_LY.Append("<td>" + dt.Rows[i]["上期二次利用卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["上期二次利用卡金额"].ToString() + "</td>");
                sbThSQ_ZKHK.Append("<td>" + dt.Rows[i]["上期二次制卡坏卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["上期二次制卡坏卡金额"].ToString() + "</td>");

                sbThBQ_RKTH.Append("<td>" + dt.Rows[i]["本期入库退换卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["本期入库退换卡金额"].ToString() + "</td>");
                sbThBQ_FCHK.Append("<td>" + dt.Rows[i]["本期返回厂家坏卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["本期返回厂家坏卡金额"].ToString() + "</td>");
                sbTHBQ_HS.Append("<td>" + dt.Rows[i]["本期二次利用回收"].ToString() + "</td><td></td><td>" + dt.Rows[i]["本期二次利用回收金额"].ToString() + "</td>");
                sbTHBQ_CK.Append("<td>" + dt.Rows[i]["本期二次利用出库"].ToString() + "</td><td></td><td>" + dt.Rows[i]["本期二次利用出库金额"].ToString() + "</td>");
                sbTHBQ_ZKHK.Append("<td>" + dt.Rows[i]["本期二次利用卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["本期二次利用卡金额"].ToString() + "</td>");
                sbTHBQ_XHRK.Append("<td>" + dt.Rows[i]["本期销毁入库"].ToString() + "</td><td></td><td>" + dt.Rows[i]["本期销毁入库金额"].ToString() + "</td>");

                sbThKC_TH.Append("<td>" + dt.Rows[i]["库存退换问题卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["库存退换问题卡金额"].ToString() + "</td>");
                sbThKC_LY.Append("<td>" + dt.Rows[i]["库存二次利用卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["库存二次利用卡金额"].ToString() + "</td>");
                sbThKC_ZKHK.Append("<td>" + dt.Rows[i]["库存二次制卡坏卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["库存二次制卡坏卡金额"].ToString() + "</td>");
            }

            sbThCard.Append("<td colspan='2'>合计</td></tr>");
            sbThNPT.Append("<td style='width:90px;'>数 量(张)</td><td style='width:90px;'>金 额(元)</td></tr>");
            sbThSQ_TH.Append("<td>" + dt.Compute("sum(上期退换问题卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(上期退换问题卡金额)", "true").ToString() + "</td></tr>");
            sbThSQ_LY.Append("<td>" + dt.Compute("sum(上期二次利用卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(上期二次利用卡金额)", "true").ToString() + "</td></tr>");
            sbThSQ_ZKHK.Append("<td>" + dt.Compute("sum(上期二次制卡坏卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(上期二次制卡坏卡金额)", "true").ToString() + "</td></tr>");

            sbThBQ_RKTH.Append("<td>" + dt.Compute("sum(本期入库退换卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(本期入库退换卡金额)", "true").ToString() + "</td></tr>");
            sbThBQ_FCHK.Append("<td>" + dt.Compute("sum(本期返回厂家坏卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(本期返回厂家坏卡金额)", "true").ToString() + "</td></tr>");
            sbTHBQ_HS.Append("<td>" + dt.Compute("sum(本期二次利用回收)", "true").ToString() + "</td><td>" + dt.Compute("sum(本期二次利用回收金额)", "true").ToString() + "</td></tr>");
            sbTHBQ_CK.Append("<td>" + dt.Compute("sum(本期二次利用出库)", "true").ToString() + "</td><td>" + dt.Compute("sum(本期二次利用出库金额)", "true").ToString() + "</td></tr>");
            sbTHBQ_ZKHK.Append("<td>" + dt.Compute("sum(本期二次利用卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(本期二次利用卡金额)", "true").ToString() + "</td></tr>");
            sbTHBQ_XHRK.Append("<td>" + dt.Compute("sum(本期销毁入库)", "true").ToString() + "</td><td>" + dt.Compute("sum(本期销毁入库金额)", "true").ToString() + "</td></tr>");

            sbThKC_TH.Append("<td>" + dt.Compute("sum(库存退换问题卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(库存退换问题卡金额)", "true").ToString() + "</td></tr>");
            sbThKC_LY.Append("<td>" + dt.Compute("sum(库存二次利用卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(库存二次利用卡金额)", "true").ToString() + "</td></tr>");
            sbThKC_ZKHK.Append("<td>" + dt.Compute("sum(库存二次制卡坏卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(库存二次制卡坏卡金额)", "true").ToString() + "</td></tr>");

            sbStr.Append(sbThCard).Append(sbThNPT).Append(sbThSQ_TH).Append(sbThSQ_LY).Append(sbThSQ_ZKHK);
            sbStr.Append(sbThBQ_RKTH).Append(sbThBQ_FCHK).Append(sbTHBQ_HS).Append(sbTHBQ_CK).Append(sbTHBQ_ZKHK).Append(sbTHBQ_XHRK);
            sbStr.Append(sbThKC_TH).Append(sbThKC_LY).Append(sbThKC_ZKHK);

            d_info.InnerHtml = sbStr.ToString() + "</table>";
        }
        #endregion

        #region 导出
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (d_info.InnerHtml.Length > 50)
            {
                ExportExcel(txt_Data.Text, d_info);
            }
            else
                ShowMessageBox("请先查询出报表内容！");
        }
        #endregion
    }
}